package com.slz3.business.module.socialManagement.socDianzan.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.socialManagement.socDianzan.domain.SocDianzan;
import com.slz3.business.module.socialManagement.socDianzan.domain.SocDianzanFactory;
import com.slz3.business.module.socialManagement.socDianzan.steward.SocDianzanSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class SocDianzanServiceImpl implements SocDianzanService {

    @Override
    public SocDianzan save(SocDianzan socDianzan) {
        return Repositorys.socDianzan.save(SocDianzanFactory.initBaseFields(socDianzan));
    }

    @Override
    public Page<SocDianzan> page(MultiValueMap<String, String> params) {
        Page<SocDianzan> socDianzanPage = Repositorys.socDianzan.findAll(
                SocDianzanSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return socDianzanPage;
    }

    @Override
    public SocDianzan modify(SocDianzan socDianzanNew) {
        return Repositorys.socDianzan.saveAndFlush(SocDianzanFactory.initBaseFields(socDianzanNew));
    }

    @Override
    public Optional<SocDianzan> single(UUID uuid) {
        return Repositorys.socDianzan.findById(uuid);
    }

    @Override
    public List<SocDianzan> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.socDianzan.findAllById(uuids);
    }

    @Override
    public List<SocDianzan> delete(List<SocDianzan> socDianzanList) {
        socDianzanList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            SocDianzanFactory.initBaseFields(item);
        });
        return Services.socDianzanTransactional.batchModify(socDianzanList);
    }

}
